package com.ruoyi.api.controller;

import com.ruoyi.api.service.VimUserApiService;
import com.ruoyi.api.vo.User;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/sys/users")
public class UserController {

    @Resource
    private VimUserApiService vimUserApiService;

    @GetMapping("{userId}")
    public AjaxResult get(@PathVariable String userId) {
        return AjaxResult.success(vimUserApiService.get(userId));
    }

    @GetMapping("my")
    public AjaxResult my() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return AjaxResult.success(vimUserApiService.get(String.valueOf(user.getUserId())));
    }

    @GetMapping("search")
    public AjaxResult search(String mobile) {
        List<User> users = vimUserApiService.search(mobile);
        if(users.size() != 0){
            return AjaxResult.success(users);
        }else {
            return AjaxResult.error("用户不存在!");
        }
    }
    /**
     * 修改用户信息
     * @param userId
     * @param user
     * @return
     */
    @PutMapping("{userId}")
    public AjaxResult update(@PathVariable String userId, @RequestBody User user) {
        user.setId(userId);
        return AjaxResult.success(vimUserApiService.update(user));
    }

    /**
     * 获取所有用户信息
     * @return
     */
    @GetMapping("getAllUser")
    public AjaxResult getAllUser(){
        List<SysUser> user = vimUserApiService.getAllUser();
        return AjaxResult.success(user);
    }

    @GetMapping("getAllUserId")
    public AjaxResult getAllUserId(){
        List<Long> list = vimUserApiService.getAllUserId();
        return AjaxResult.success(list);
    }
}
